package scale.GUI;

import java.awt.ComponentOrientation;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.text.DecimalFormat;

import javax.swing.JPanel;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import scale.data.Scale;

public class Brutto extends JPanel {
	
	Scale scale;
	JSpinner s;
	final DecimalFormat dFormat = new DecimalFormat("#.###");
	
	public Brutto(Scale sc){
		this.scale = sc;
		this.setComponentOrientation(ComponentOrientation.LEFT_TO_RIGHT);
		this.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		double min = 0.000;  
        double value = 0.000;  
        double max = 1000;  
        double stepSize = 0.001;  
        SpinnerNumberModel model = new SpinnerNumberModel(value, min, max, stepSize);  
		s = new JSpinner(model);
		JSpinner.NumberEditor editor = (JSpinner.NumberEditor)s.getEditor();  
        DecimalFormat format = editor.getFormat();  
        format.setMinimumFractionDigits(3);  
        Dimension d = s.getPreferredSize();
		c.ipadx = 50;
		c.ipady = 10;
		c.anchor = GridBagConstraints.LAST_LINE_START;
		
		s.addChangeListener(new ChangeListener() {
			
			@Override
			public void stateChanged(ChangeEvent e) {
				getBrutto(new Double(dFormat.format(s.getValue()).replaceAll(",", ".")));
			}
		});
		
		this.add(s,c);
		
	}
	
	public void getBrutto(double b){
		scale.setBrutto(b);
	}
	
	public void setBrutto(double b){
		s.setValue(b);
	}
	
}